//
// Created by Snow on 2023/3/13.
//
#include<cstdio>
#include<cstdlib>
#define MaxSize 10
typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];//存放队中元素
    int front,rear;//队头和队尾指针
}SqQueue;
void InitQueue(SqQueue *&q)
{
    q=(SqQueue *)malloc(sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroySqQueue(SqQueue *&q)
{
    free(q);
}
bool QueueEmpty(SqQueue *q)
{
    return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
    if((q->rear+1)%MaxSize==q->front)//队满上溢出
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
    if(q->front==q->rear)//队空下溢出
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}
int Count(SqQueue *q)
{
    return(q->rear-q->front+MaxSize)%MaxSize;
}
bool disQueue(SqQueue *q)
{
    if(q->front==q->rear)//队空下溢出
        return false;
    int i=q->front;
    do
    {
        i=(i+1)%MaxSize;
        printf("%d ",q->data[i]);
    } while (i!=q->rear);
    printf("\n");
    return true;
}
int main()
{
    int i;
    ElemType e,arr[]={1,2,3,4,5,'\0'};
    SqQueue *q;
    InitQueue(q);
    if(QueueEmpty(q))printf("Empty\n");
    for(i=0;arr[i]!='\0';i++)
        enQueue(q,arr[i]);
    disQueue(q);
    printf("Count=%d\n", Count(q));
    deQueue(q,e);
    printf("%d\n",e);
    disQueue(q);
    DestroySqQueue(q);
}